package com.example.administrator.im.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.administrator.im.config.PhoneParam;

/**
 * create:2018/9/16 0:54
 * author: 姚鸿杰
 **/
public class IM_Item extends HorizontalScrollView {

    private boolean isCall = false;

    public IM_Item(Context context) {
        super(context);
    }

    public IM_Item(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (!isCall) {
            ViewGroup vg = (ViewGroup) getChildAt(0);
            View v = vg.getChildAt(0);
//            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(PhoneParam.getInstance(getContext()).getS_screen_width(),
//                    LayoutParams.MATCH_PARENT);
//            v.setLayoutParams(lp);
            v.getLayoutParams().width = PhoneParam.getInstance(getContext()).getS_screen_width();

            for (int i = 0; i < vg.getChildCount() - 1; i++) {
                View b = vg.getChildAt(i + 1);
                b_width += b.getMeasuredWidth();
            }
            isCall = true;
        }
    }

    //手指按下时的位置
    private int pressX;
    //手指移动时的位置
    private int moveX;
    //右部控件的总宽度
    private int b_width;
    //判断是否打开右边菜单
    private static boolean is_open = false;
    //手指按下时的时间
    private long press_time;
    //快速滑动时间
    private long fast_slide = 500;
    //保存上个点击的item
    private static IM_Item im_item;

    public static boolean isIs_open() {
        return is_open;
    }


    public static IM_Item getIm_item() {
        return im_item;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (is_open && im_item != this) {
                im_item.closeMenu();
                return false;
            }
            im_item = this;
            pressX = (int) ev.getX();
            press_time = System.currentTimeMillis();
        }

        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                moveX = pressX - (int) ev.getX();
                break;
            case MotionEvent.ACTION_UP:

                if (moveX == 0) return true;

                long up_time = System.currentTimeMillis();
                if (up_time - press_time <= fast_slide) {
                    if (moveX > 0) openMenu();
                    else closeMenu();
                    return true;
                }

                if (Math.abs(moveX) >= b_width / 2) {
                    if (is_open) closeMenu();
                    else openMenu();

                } else {
                    if (!is_open) closeMenu();
                    else openMenu();
                }
                return true;
        }
        return super.onTouchEvent(ev);
    }


    /**
     * 打开菜单
     */
    private void openMenu() {
        smoothScrollTo(PhoneParam.getInstance(getContext()).getS_screen_width(), 0);
        is_open = true;
    }

    /**
     * 关闭菜单
     */
    public void closeMenu() {
        smoothScrollTo(0, 0);
        is_open = false;
    }

}
